﻿ 
(function (i, s, o, g, r, a, m) {
    i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
        (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date(); a = s.createElement(o),
    m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');

ga('create', 'UA-47498836-1', '');
ga('send', 'pageview');


var macros = null;

document.addEventListener('DOMContentLoaded', function () {
  
    chrome.tabs.query({
        active: true,               // Select active tabs
        lastFocusedWindow: true     // In the current window
    }, function (array_of_Tabs) {
        
        // Since there can only be one active tab in one active window, 
        //  the array has only one element
        var tab = array_of_Tabs[0];

       
        chrome.runtime.sendMessage({ command: "get", tab: tab }, function (response) {
            
            
           
            var res = response.result.entries;
           
            if (res != null) {
                $("#ScripterFoundNotFound").hide();
                $("#ScripterFound").show();
              
               
                macros = response.result.macros;
                var tmodel = new model(response.result.entries);
                
                ko.applyBindings(tmodel);
            }
            else {
                $("#ScripterFound").hide();
                $("#ScripterFoundNotFound").show();
            }

        });
    });

 
   
});

function model(data) {
  
    var self = this;
    self.logs = ko.observableArray(data.logs);
    self.errors = ko.observableArray(data.errors);
    self.page = ko.observable(data.page);
 
    var filesarray = objectsToArray(data.files);
 
    for (var i = 0; i < filesarray.length; i++) {
        filesarray[i].cleanname = filesarray[i].name.replace(/\s+/g, '-').toLowerCase();      
        filesarray[i].blocks = objectsToArray(filesarray[i].blocks);
        
        for (var t = 0; t < filesarray[i].blocks.length; t++) {         
            
            filesarray[i].blocks[t].rules = objectsToArray(filesarray[i].blocks[t].rules);
            for (var y = 0; y < filesarray[i].blocks[t].rules.length; y++) {
                for (var re = 0; re < filesarray[i].blocks[t].rules[y].RuleExpressions.length; re++) {                 
                    
                    filesarray[i].blocks[t].rules[y].RuleExpressions[re].Macro = macros[ filesarray[i].blocks[t].rules[y].RuleExpressions[re].Macro];
                }
            }
        }
        
        filesarray[i].blocks = ko.observable(objectsToArray(filesarray[i].blocks));
        
    }
      
 
    self.files = ko.observableArray(filesarray);
 
}



function objectsToArray(data) {
    var resarray = [];
    for (var i in data)
        resarray.push(data[i]);
    return resarray;
}



 
$(document).ready(function () {
    $(document).on('click', "li.filenode", function () {       
        $(this).children(".blocks").toggle();
    });

    $(document).on('click', ".plus", function () {
        $(this).parents("section").siblings("section").toggleClass("hide");
    
        $(this).children(".glyphicon").toggleClass("glyphicon-chevron-right glyphicon-chevron-down");
    });
    


    
})

 

